package com.iot.controller;

import com.iot.core.LResult;
import com.iot.core.PageData;
import com.iot.mapper.DRefMapper;
import com.iot.service.chart.ChartService;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/chart")
public class ChartController extends BaseController {
    @Autowired
    private ChartService service;
    @Autowired
    private DRefMapper dRefMapper;

    @ResponseBody
    @RequestMapping("getDevChart")
    public String getDevChart() {

        try {
            PageData map = getPageData();
            if (!map.containsKey("stype")) {
                map.put("stype", 0);
            }
            if (!map.containsKey("typeId")) {
                map.put("typeId", 0);
            }
            if (!map.containsKey("areaId")) {
                map.put("areaId", 0);
            }
            if (!map.containsKey("regionId")) {
                map.put("regionId", 0);
            }
            if (!map.containsKey("siteId")) {
                map.put("siteId", 0);
            }
            List<Map<String, Object>> list = service.getDevChart(map);
            return LResult.Success(list).toJson();
        } catch (Exception e) {
            e.printStackTrace();
            return LResult.Error(e.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("getDevOpenTimesChart")
    public String getDevOpenTimesChart() {
        try {
            PageData map = getPageData();
            if (!map.containsKey("deviceId") || Common.isEmpty(map.get("deviceId"))) {
                throw new Exception("没有设备信息，无法统计");
            }
            if (!map.containsKey("typeId") || Common.isEmpty(map.get("typeId"))) {
                throw new Exception("没有设备类别，无法统计");
            }
            if (!map.containsKey("stype")) {
                throw new Exception("没有日期统计方式，无法统计");
            } else {
                String stype = map.get("stype") + "";
                int syear = Common.toInt(map.get("year"));
                String smonth = map.get("month") + "";
                if (stype.equals("w")) {
                    Date date = new Date();
                    map.put("sdate", Common.getShortDate(Common.getSomeDay(date, -7)));
                    map.put("edate", Common.getShortDate(Common.getSomeDay(date, 1)));
                } else if (stype.equals("m")) {
                    Date date = new Date();
                    map.put("sdate", Common.getShortDate(Common.getSomeDay(date, -30)));
                    map.put("edate", Common.getShortDate(Common.getSomeDay(date, 1)));
                } else if (stype.equals("y")) {
                    //String year = Common.getYear();
                    map.put("sdate", syear + "-01-01");
                    map.put("edate", (syear + 1) + "-01-01");
                } else if (stype.equals("ym")) {
                    //String year = Common.getMonth();
                    int month = (Integer.parseInt(smonth) + 1);
                    String emonth = month < 10 ? ("0" + month) : month + "";
                    map.put("sdate", syear + "-" + smonth + "-01");
                    map.put("edate", syear + "-" + emonth + "-01");
                } else if (stype.equals("r")) {
                    if (!map.containsKey("sdate")) {
                        throw new Exception("没有开始日期，无法统计");
                    }
                    if (!map.containsKey("edate")) {
                        throw new Exception("没有结束日期，无法统计");
                    } else {
                        String strSdate = map.get("edate").toString();
                        String tempdate = Common.getShortDate(Common.getSomeDay(strSdate, 1));
                        map.put("edate", tempdate);
                    }
                }
            }
            List<Map<String, Object>> list = service.getDevOpenTimesChart(map);
            return LResult.Success(list).toJson();
        } catch (Exception e) {
            e.printStackTrace();
            return LResult.Error(e.getMessage()).toJson();
        }
    }


    @ResponseBody
    @RequestMapping("getDevSensorChart")
    public String getDevSensorChart() {
        try {
            PageData map = getPageData();

            if (!map.containsKey("deviceId") || Common.isEmpty(map.get("deviceId"))) {
                throw new Exception("没有设备，无法统计");
            }
            if (!map.containsKey("sensorId") || Common.isEmpty(map.get("sensorId"))) {
                throw new Exception("没有设备传感器，无法统计");
            }
            if (!map.containsKey("stype")) {
                throw new Exception("没有日期统计方式，无法统计");
            } else {
                String stype = map.get("stype") + "";
                if (stype.equals("w")) {
                    Date date = new Date();
                    map.put("sdate", Common.getShortDate(Common.getSomeDay(date, -7)));
                    map.put("edate", Common.getShortDate(Common.getSomeDay(date, 1)));
                } else if (stype.equals("m")) {
                    Date date = new Date();
                    map.put("sdate", Common.getShortDate(Common.getSomeDay(date, -30)));
                    map.put("edate", Common.getShortDate(Common.getSomeDay(date, 1)));
                } else if (stype.equals("y")) {
                    String year = Common.getYear();
                    map.put("sdate", year + "-01-01");
                    map.put("edate", (Integer.parseInt(year) + 1) + "-01-01");
                } else if (stype.equals("r")) {
                    if (!map.containsKey("sdate")) {
                        throw new Exception("没有开始日期，无法统计");
                    }
                    if (!map.containsKey("edate")) {
                        throw new Exception("没有结束日期，无法统计");
                    } else {
                        String strSdate = map.get("edate").toString();
                        String tempdate = Common.getShortDate(Common.getSomeDay(strSdate, 1));
                        map.put("edate", tempdate);
                    }
                }
            }
            List<Map<String, Object>> list = service.getDevSensorChart(map);
            return LResult.Success(list.size(), list).toJson();
        } catch (Exception e) {
            e.printStackTrace();
            return LResult.Error(e.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initDevSensorChart")
    public String initDevSensorChart(int cityId, int appId) {
        try {
            HashMap<String, Object> mapData = new HashMap<>();
            mapData.put("dtype", dRefMapper.getDeviceType());
            mapData.put("area", dRefMapper.getArea(cityId));
            // mapData.put("sensor",dRefMapper.getSensor());
            return LResult.Success(mapData).toJson();
        } catch (Exception e) {
            e.printStackTrace();
            return LResult.Error(e.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initDevBoxWarnChart")
    public String initDevBoxWarnChart(int cityId, int appId) throws Exception {
        if (cityId<=0) {
            throw new Exception("没有市/区信息无法查询");
        }
        try {
            HashMap<String, Object> mapData = new HashMap<>();
//            mapData.put("dtype", dRefMapper.getDeviceType());
            mapData.put("area", dRefMapper.getArea(cityId));
            // mapData.put("sensor",dRefMapper.getSensor());
            return LResult.Success(mapData).toJson();
        } catch (Exception e) {
            e.printStackTrace();
            return LResult.Error(e.getMessage()).toJson();
        }
    }



    @ResponseBody
    @RequestMapping("getDevBoxWarnChart")
    public String getDevBoxWarnChart() {
        try {
            PageData map = getPageData();
            if (!map.containsKey("deviceId") || Common.isEmpty(map.get("deviceId"))) {
                throw new Exception("没有设备信息，无法统计");
            }
            if (!map.containsKey("wtype") || Common.isEmpty(map.get("wtype"))) {
                throw new Exception("没有告警类型，无法统计");
            }
//            if (!map.containsKey("typeId")) {
//                throw new Exception("没有设备类别，无法统计");
//            }
            if (!map.containsKey("stype")) {
                throw new Exception("没有日期统计方式，无法统计");
            } else {
                String stype = map.get("stype") + "";
                int syear = Common.toInt(map.get("year"));
                String smonth = map.get("month") + "";
                if (stype.equals("w")) {
                    Date date = new Date();
                    map.put("sdate", Common.getShortDate(Common.getSomeDay(date, -7)));
                    map.put("edate", Common.getShortDate(Common.getSomeDay(date, 1)));
                } else if (stype.equals("m")) {
                    Date date = new Date();
                    map.put("sdate", Common.getShortDate(Common.getSomeDay(date, -30)));
                    map.put("edate", Common.getShortDate(Common.getSomeDay(date, 1)));
                } else if (stype.equals("y")) {
                    //String year = Common.getYear();
                    map.put("sdate", syear + "-01-01");
                    map.put("edate", (syear + 1) + "-01-01");
                } else if (stype.equals("ym")) {
                    //String year = Common.getMonth();
                    int month = (Integer.parseInt(smonth) + 1);
                    String emonth = month < 10 ? ("0" + month) : month + "";
                    map.put("sdate", syear + "-" + smonth + "-01");
                    map.put("edate", syear + "-" + emonth + "-01");
                } else if (stype.equals("r")) {
                    if (!map.containsKey("sdate")) {
                        throw new Exception("没有开始日期，无法统计");
                    }
                    if (!map.containsKey("edate")) {
                        throw new Exception("没有结束日期，无法统计");
                    } else {
                        String strSdate = map.get("edate").toString();
                        String tempdate = Common.getShortDate(Common.getSomeDay(strSdate, 1));
                        map.put("edate", tempdate);
                    }
                }
            }
            List<Map<String, Object>> list = new ArrayList<>();
            String wtype = map.get("wtype").toString();
            if (wtype.equals("wd")) {
                map.put("sensorId", 4);
                list = service.getDevBoxWarnChart1(map);
            } else if (wtype.equals("sd")) {
                map.put("sensorId", 5);
                list = service.getDevBoxWarnChart1(map);
            } else if (wtype.equals("qs")) {
                map.put("sensorId", 2);
                list = service.getDevBoxWarnChart1(map);
            } else if (wtype.equals("dld")) {
                map.put("sensorId", 3);
                list = service.getDevBoxWarnChart1(map);
            } else if (wtype.equals("ddy")) {
                map.put("sensorId", 15);
                list = service.getDevBoxWarnChart1(map);
            } else if (wtype.equals("qx")) {
                map.put("sensorId", 6);
                list = service.getDevBoxWarnChart1(map);
            }
           else  if (wtype.equals("xh")) {
                list = service.getDevBoxWarnChart3(map);
            }
            else if (wtype.equals("do2")) {
                map.put("w2", "A.DOOR_OPEN_STATUS");
                list = service.getDevBoxWarnChart2(map);
            }
            else if (wtype.equals("lo2")) {
                map.put("w2", "A.LOCK_OPEN_STATUS");
                list = service.getDevBoxWarnChart2(map);
            }
            else if (wtype.equals("dt2")) {
                map.put("w2", "A.DOOR_TIME_STATUS");
                list = service.getDevBoxWarnChart2(map);
            }
            else if (wtype.equals("lt2")) {
                map.put("w2", "A.LOCK_TIME_STATUS");
                list = service.getDevBoxWarnChart2(map);
            }
            return LResult.Success(list).toJson();
        } catch (Exception e) {
            e.printStackTrace();
            return LResult.Error(e.getMessage()).toJson();
        }
    }





    @ResponseBody
    @RequestMapping("initDevMCoverWarnChart")
    public String initDevMCoverWarnChart(int cityId, int appId) throws Exception {
        if (cityId<=0) {
            throw new Exception("没有市/区信息无法查询");
        }
        try {
            HashMap<String, Object> mapData = new HashMap<>();
//            mapData.put("dtype", dRefMapper.getDeviceType());
            mapData.put("area", dRefMapper.getArea(cityId));
            // mapData.put("sensor",dRefMapper.getSensor());
            return LResult.Success(mapData).toJson();
        } catch (Exception e) {
            e.printStackTrace();
            return LResult.Error(e.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("getDevMCoverWarnChart")
    public String getDevMCoverWarnChart() {
        try {
            PageData map = getPageData();
            if (!map.containsKey("deviceId") || Common.isEmpty(map.get("deviceId"))) {
                throw new Exception("没有设备信息，无法统计");
            }
            if (!map.containsKey("wtype") || Common.isEmpty(map.get("wtype"))) {
                throw new Exception("没有告警类型，无法统计");
            }
//            if (!map.containsKey("typeId")) {
//                throw new Exception("没有设备类别，无法统计");
//            }
            if (!map.containsKey("stype")) {
                throw new Exception("没有日期统计方式，无法统计");
            } else {
                String stype = map.get("stype") + "";
                int syear = Common.toInt(map.get("year"));
                String smonth = map.get("month") + "";
                if (stype.equals("w")) {
                    Date date = new Date();
                    map.put("sdate", Common.getShortDate(Common.getSomeDay(date, -7)));
                    map.put("edate", Common.getShortDate(Common.getSomeDay(date, 1)));
                } else if (stype.equals("m")) {
                    Date date = new Date();
                    map.put("sdate", Common.getShortDate(Common.getSomeDay(date, -30)));
                    map.put("edate", Common.getShortDate(Common.getSomeDay(date, 1)));
                } else if (stype.equals("y")) {
                    //String year = Common.getYear();
                    map.put("sdate", syear + "-01-01");
                    map.put("edate", (syear + 1) + "-01-01");
                } else if (stype.equals("ym")) {
                    //String year = Common.getMonth();
                    int month = (Integer.parseInt(smonth) + 1);
                    String emonth = month < 10 ? ("0" + month) : month + "";
                    map.put("sdate", syear + "-" + smonth + "-01");
                    map.put("edate", syear + "-" + emonth + "-01");
                } else if (stype.equals("r")) {
                    if (!map.containsKey("sdate")) {
                        throw new Exception("没有开始日期，无法统计");
                    }
                    if (!map.containsKey("edate")) {
                        throw new Exception("没有结束日期，无法统计");
                    } else {
                        String strSdate = map.get("edate").toString();
                        String tempdate = Common.getShortDate(Common.getSomeDay(strSdate, 1));
                        map.put("edate", tempdate);
                    }
                }
            }
            List<Map<String, Object>> list = new ArrayList<>();
            String wtype = map.get("wtype").toString();
            if (wtype.equals("wd")) {
                map.put("sensorId", 12);
                list = service.getDevMCoverWarnChart1(map);
            } else if (wtype.equals("sd")) {
                map.put("sensorId", 13);
                list = service.getDevMCoverWarnChart1(map);
            } else if (wtype.equals("yw")) {
                map.put("sensorId", 10);
                list = service.getDevMCoverWarnChart1(map);
//            } else if (wtype.equals("dld")) {
//                map.put("sensorId", 3);
//                list = service.getDevBoxWarnChart1(map);
            } else if (wtype.equals("ddy")) {
                map.put("sensorId", 8);
                list = service.getDevMCoverWarnChart1(map);
            } else if (wtype.equals("qx")) {
                map.put("sensorId", 17);
                list = service.getDevMCoverWarnChart1(map);
            }
            else if (wtype.equals("yhqt")) {
                map.put("sensorId", 14);
                list = service.getDevMCoverWarnChart1(map);
            }
            else if (wtype.equals("wy")) {
                map.put("sensorId", 18);
                list = service.getDevMCoverWarnChart1(map);
            }
            else if (wtype.equals("yanw")) {
                map.put("sensorId", 16);
                list = service.getDevMCoverWarnChart1(map);
            }
            else  if (wtype.equals("xh")) {
                list = service.getDevMCoverWarnChart3(map);
            }

            return LResult.Success(list).toJson();
        } catch (Exception e) {
            e.printStackTrace();
            return LResult.Error(e.getMessage()).toJson();
        }
    }
}
